package cn.gx.learn.security_boot_jwt_websocket.aop.login;

import cn.gx.learn.security_boot_jwt_websocket.aop.dal.date.Log;
import cn.gx.learn.security_boot_jwt_websocket.aop.service.LogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.ThreadPoolExecutor;

@Component
@Aspect
@Slf4j
public class LogAOP {

    @Resource(name = "threadpool")
    private ThreadPoolExecutor logthreadpool;
    @Autowired
    private LogService logService;


    @Around("execution(* cn.gx.learn.security_boot_jwt_websocket.base.controller.UserController.login(..))")
    public Object beforeLogin(ProceedingJoinPoint joinPoint) {
        Object result = null;
        boolean success = false;
        try {
            result =joinPoint.proceed();
            ResponseEntity<?>responseEntity=(ResponseEntity<?>) result;
            success=responseEntity.getStatusCode().is2xxSuccessful();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            Object[] args = joinPoint.getArgs();
            String recard = joinPoint.getSignature().getName()+ Arrays.toString(args);
            log.info("方法："+joinPoint.getSignature().getName()+"    参数:"+Arrays.toString(args));
            Log log = new Log(recard);
            log.setType(Log.Menutype.Login);

            log.setStatus(success?1:0);
            logthreadpool.submit(()->{
                long starttime=System.currentTimeMillis();
                try {
                    logService.save(log);
                    logService.logTaskCompletion(logthreadpool,starttime,true,null);
                } catch (Exception e) {
                    logService.logTaskCompletion(logthreadpool,starttime,false,e);
                    throw new RuntimeException(e);
                }

            });
        }

        return result;
    }

    @Around("execution(* cn.gx.learn.security_boot_jwt_websocket.base.controller.UserController.register(..))")
    public Object beforeRegister(ProceedingJoinPoint joinPoint) {
        Object result = null;
        boolean success = false;
        try {
            result =joinPoint.proceed();
            ResponseEntity<?>responseEntity=(ResponseEntity<?>) result;
            success="success".equals(responseEntity.getBody());
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            Object[] args = joinPoint.getArgs();
            String recard = joinPoint.getSignature().getName()+ Arrays.toString(args);
            log.info("方法："+joinPoint.getSignature().getName()+"    参数:"+Arrays.toString(args));
            Log log = new Log(recard);
            log.setType(Log.Menutype.Register);

            log.setStatus(success?1:0);
            logthreadpool.submit(()->{
                long starttime=System.currentTimeMillis();
                try {
                    logService.save(log);
                    logService.logTaskCompletion(logthreadpool,starttime,true,null);
                } catch (Exception e) {
                    logService.logTaskCompletion(logthreadpool,starttime,false,e);
                    throw new RuntimeException(e);
                }

            });
        }

        return result;
    }

    @Around("execution(* cn.gx.learn.security_boot_jwt_websocket.appiontment.controller.AppiontmentController.*(..))")
    public Object beforeAppiontmentController(ProceedingJoinPoint joinPoint) {
        Object result = null;
        boolean success = false;
        try {
            result=joinPoint.proceed();
            ResponseEntity<?>responseEntity = (ResponseEntity<?>)result;
            success="success".equals(responseEntity.getBody());
            System.out.println(result.toString());

            System.out.println(success);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            Object[] args = joinPoint.getArgs();
            String recard = joinPoint.getSignature().getName()+ Arrays.toString(args);
            log.info("方法："+joinPoint.getSignature().getName()+"    参数:"+Arrays.toString(args));
            Log log = new Log(recard);
            if(joinPoint.getSignature().getName().equals("create")){
                log.setType(Log.Menutype.APPIONTMENT);
            }
            log.setStatus(success?1:0);
            logthreadpool.submit(()->{
                long starttime=System.currentTimeMillis();
                try {
                    logService.save(log);
                    logService.logTaskCompletion(logthreadpool,starttime,true,null);
                }
                catch (Exception e) {
                    logService.logTaskCompletion(logthreadpool,starttime,false,e);
                    throw new RuntimeException(e);
                }
            });

        }
        return result;


    }




}
